<?php

namespace app\ruinuo\design;

class Logic
{
    public function DesignTable($form = [])
    {
        $str = '';
        foreach ($form as $k => $v) {
            if ($v['tag'] == 'designtable') {
                $str_field = '';
                $cols = !empty($v['cols']) ? $v['cols'] : [];
                foreach ($cols as $k3 => $v3) {
                    $str_field .= $v3['field'] . ',';
                }
                $str .= "            \$" . $v['field'] . "=Db::name('" . $v['database'] . "')->where('is_delete=0 and " . $v['database_field'] . "='. \$params['id'])->field('" . rtrim($str_field, ',') . ",id,create_time,create_id,update_time,update_id')->select()->toArray();" . PHP_EOL;
                $str .= "            \$find['" . $v['field'] . "']=!empty(\$" . $v['field'] . ") ? json_encode(\$" . $v['field'] . ", JSON_UNESCAPED_UNICODE) : '[]';" . PHP_EOL;
            }
        }
        return $str;
    }

    public function FormData($form = [], $mode = [])
    {
        $str = '';
        foreach ($form as $k => $v) {
            if (!empty($v['field'])) {
                $ge = '';
                if ($str) {
                    $ge = "        ";
                }
                $c = explode('__', $v['field']);
                $table = "a";
                $field = $v['field'];
                if (count($c) == 2) {
                    $table = $c[0];
                    $field = $c[1];
                }
                if ($v['tag'] == 'ordernum') {
                    $rule_str = '';
                    foreach ($v['num_rule'] as $k3 => $v3) {
                        $rule_str .= "            ['type'=>'" . $v3['rule_type'] . "','val'=>'" . $v3['rule_val'] . "']," . PHP_EOL;
                    }
                    if ($rule_str) {
                        $rule_str = '$rule=[' . PHP_EOL . $rule_str . '        ];' . PHP_EOL;
                    } else {
                        $rule_str = '$rule=[];' . PHP_EOL;
                    }
                    $str .= $ge . $rule_str;
                    $str .= $ge . "        if (empty(\$params['id'])){" . PHP_EOL;
                    $str .= "            \$data['" . $table . "']['" . $field . "']=\$LogicService->GetNumRule(\$rule,'" . $mode['database'] . "');" . PHP_EOL;
                    $str .= "        }" . PHP_EOL;
                } else {
                    if ($v['tag'] != 'password') {
                        if (strlen($v['value']) == 0) {
                            $value = "NULL";
                        } else {
                            $value = "'" . $v['value'] . "'";
                        }
                        $str .= $ge . "if (isset(\$params['" . $v['field'] . "'])){" . PHP_EOL;
                        $str .= "            \$data['" . $table . "']['" . $field . "']=strlen(\$params['" . $v['field'] . "']) ? \$params['" . $v['field'] . "'] : " . $value . ";" . PHP_EOL;
                        $str .= "        }" . PHP_EOL;
                    } else {
                        $str .= $ge . "if (!empty(\$params['" . $v['field'] . "'])){" . PHP_EOL;
                        $str .= "            \$data['" . $table . "']['" . $field . "']=md5(\$params['" . $v['field'] . "']);" . PHP_EOL;
                        $str .= "        }" . PHP_EOL;
                    }
                }
            }
        }
        return $str;
    }


    public function DetailField($form = [])
    {
        $str = '';
        foreach ($form as $k => $v) {
            if (!empty($v['field'])) {
                $c = explode('__', $v['field']);
                if (count($c) == 1) {
                    $field = 'a.' . $v['field'];
                } else {
                    $field = $c[0] . '.' . $c[1] . ' as ' . $v['field'];
                }
                if ($v['field'] && $v['tag'] != 'password' && $v['tag'] != 'subtable' && $v['tag'] != 'designtable') {
                    $str .= $field . ",";
                }
            }
        }
        $str = "\$field='" . $str . "a.id,a.create_time,a.update_time,a.create_id,a.update_id';";
        return $str;
    }

    public function DetailModel($mode = [])
    {
        $str = "\$Model->alias('a')";
        $sub_database = !empty($mode['sub_database']) ? json_decode($mode['sub_database'], true) : [];
        foreach ($sub_database as $k => $v) {
            if ($v['type'] == 1) {
                $str .= PHP_EOL . "                ->leftJoin('" . $v['database'] . " " . $v['alias'] . "', 'a." . $v['key_field'] . "= " . $v['alias'] . "." . $v['sub_field'] . "')";
            } else {
                $str .= PHP_EOL . "                ->Join('" . $v['database'] . " " . $v['alias'] . "', 'a." . $v['sub_field'] . "= " . $v['alias'] . "." . $v['key_field'] . "')";
            }
        }
        return $str;
    }

    public function OptionsNum($form = [])
    {
        $str = '';
        foreach ($form as $k => $v) {
            if (!empty($v['options_num'])) {
                $str .= $v['options_num'] . ',';
            }
            if (!empty($v['cols'])) {
                foreach ($v['cols'] as $k2 => $v2) {
                    if (!empty($v2['options'])) {
                        $str .= $v2['options'] . ',';
                    }
                }
            }
        }
        return rtrim($str, ",");
    }

    public function GetVerify($form = [], $mode = [])
    {
        $str = '';
        $newField = [];
        foreach ($form as $k => $v) {
            if (!empty($v['field'])) {
                $newField[$v['field']] = $v;
            }
        }
        foreach ($form as $k => $v) {
            if (!empty($v['field'])) {
                if ($v['only'] == 1) {
                    $base = $this->FindDataBase($v['field'], $mode);
                    $table = $base['database'];
                    $sub_field = $base['sub_field'];
                    $key_field = $base['key_field'];
                    $base_type = $base['type'];
                    $key_table = $mode['database'];
                    $str .= "            ['field'=>'" . $v['field'] . "','title'=>'" . $v['title'] . "','rule'=>'only','type'=>'key','tag'=>'" . $v['tag'] . "','key_table'=>'" . $key_table . "','table'=>'" . $table . "','sub_field'=>'" . $sub_field . "','key_field'=>'" . $key_field . "','base_type'=>'" . $base_type . "']," . PHP_EOL;
                }
                if ($v['required'] == 1) {
                    $str .= "            ['field'=>'" . $v['field'] . "','title'=>'" . $v['title'] . "','rule'=>'required','type'=>'key','tag'=>'" . $v['tag'] . "']," . PHP_EOL;
                }
                if ($v['verify']) {
                    $str .= "            ['field'=>'" . $v['field'] . "','title'=>'" . $v['title'] . "','rule'=>'" . $v['verify'] . "','type'=>'key','tag'=>'" . $v['tag'] . "']," . PHP_EOL;
                }
            }

            if (!empty($v['cols'])) {
                foreach ($v['cols'] as $k2 => $v2) {
                    $required = !empty($v2['required']) ? $v2['required'] : 2;
                    if ($required == 1) {
                        $str .= "            ['field'=>'" . $v2['field'] . "','title'=>'" . $v2['title'] . "','rule'=>'required','path'=>'" . $v['field'] . "','type'=>'json']," . PHP_EOL;
                    }
                    if (!empty($v2['verify'])) {
                        $str .= "            ['field'=>'" . $v2['field'] . "','title'=>'" . $v2['title'] . "','rule'=>'" . $v2['verify'] . "','path'=>'" . $v['field'] . "','type'=>'json']," . PHP_EOL;
                    }
                }
            }
            if (!empty($v['event_val'])) {

                foreach ($v['event_val'] as $k2 => $v2) {
                    $required = !empty($v2['required']) ? $v2['required'] : 2;
                    if ($required == 1) {

                        $str .= "            ['field'=>'" . $v2['event_field'] . "','title'=>'" . $newField[$v2['event_field']]['title'] . "','rule'=>'required','event_type'=>'" . $v2['event_type'] . "','event_val'=>'" . $v2['event_val'] . "','event_field'=>'" . $v['field'] . "','type'=>'event']," . PHP_EOL;
                    }
                }
            }
        }
        if ($str) {
            $str = '$data=[' . PHP_EOL . $str . '        ];';
        } else {
            $str = '$data=[];';
        }
        return $str;

    }


    public function DataBase($subTable = [], $sub_database = [])
    {

        $str = '';
        foreach ($sub_database as $k => $v) {
            $str .= "            ['database'=>'" . $v['database'] . "','alias'=>'" . $v['alias'] . "','tag'=>'one','type'=>'" . $v['type'] . "','sub_field'=>'" . $v['sub_field'] . "','key_field'=>'" . $v['key_field'] . "']," . PHP_EOL;
        }
        foreach ($subTable as $k => $v) {
            $str .= "            ['database'=>'" . $v['database'] . "','field'=>'" . $v['field'] . "','tag'=>'" . $v['tag'] . "','sub_id'=>'" . $v['sub_id'] . "','str_field'=>'" . $v['str_field'] . "']," . PHP_EOL;
        }
        if ($str) {
            $str = '$data=[' . PHP_EOL . '' . $str . '        ];';
        } else {
            $str = '$data=[];';
        }
        return $str;
    }


    public function FindDataBase($field = '', $mode = [])
    {
        $sub_database = !empty($mode['sub_database']) ? json_decode($mode['sub_database'], true) : [];
        $c = explode('__', $field);
        $data = [
            'database' => $mode['database'],
            'key_field' => '',
            'sub_field' => '',
            'type' => '',
        ];

        if (count($c) == 2) {
            foreach ($sub_database as $k => $v) {
                if ($c[0] == $v['alias']) {
                    $data = [
                        'database' => $v['database'],
                        'key_field' => $v['key_field'],
                        'sub_field' => $v['sub_field'],
                        'type' => $v['type'],
                    ];
                }
            }
        }
        return $data;

    }


}